package service.exportation;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.stream.XMLStreamException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class ExportationStarter implements InitializingBean {
	private IExporter exporter;

	
	public static void main(String... args) {
		if(args.length != 1) {
			System.out.println("Un nom de répertoire est requis en argument.");
			return;
		}
		
		ExportationStarter app = new ExportationStarter();
		app.start(args[0]);
	}
	
	
	public void start(String folderPath) {
		Logger.getLogger("").setLevel(Level.SEVERE);
		System.out.println("DEBUT export des données");

	    ApplicationContext ac = new ClassPathXmlApplicationContext("/spring.xml");
	    AutowireCapableBeanFactory acbf = ac.getAutowireCapableBeanFactory();

	    exporter = acbf.getBean(IExporter.class);
		
	    try {	    	
	    	System.out.println("Repertoire d'export : " + folderPath);
			exporter.export(folderPath);
			System.out.println("FIN export des données");
		} catch (XMLStreamException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	public void afterPropertiesSet() throws Exception {

	}
}